use "$LocalData\BartikAnalyticData_2digit.dta", clear

* Set panel variables
xtset geography year

*Generating year-industry-shares for computation for Bartik analytics and SEs
		keep if year>2002  //Restricts to period of analysis
		
		levelsof year, local(y) //Creates a macro list of all years
		
		foreach year in `y' {
			foreach ind_var of varlist c_i_share_2000* {
				qui gen t`year'_`ind_var' = `ind_var' * (year == `year')
				}
			}
	
		qui tab year, gen(year_) //Generates year dummies
		drop year_1
		qui tab geography, gen(geography_) //Generates year dummies
		drop geography_1
		

/******************************************************************************/
/******************************************************************************/
/* Regressions                                                                */
/******************************************************************************/
/******************************************************************************/

local years  (year>2002) & abs(gsmall_dep1)<5 & gSmallFirm1!=.
*local controls ln_pop_2000 unem_2000 urban_2000 lnincome2000 lnbr2000 small_dep_share2000
local controls 	dlngdp unem_change pop_growth annualchange 

gen testconditions = cond(`years',1,0)
bysort geography: egen insample = sum(test)
keep if insample>1







*Mergers
noi display "Starting merger tests."


xtset geography year

		gen Lcl_dep = L.cl_dep
		gen LmHQ = L.mHQ
		gen Lacq_dep = L.acq_dep
		
		

local years  (year>2002) & abs(gsmall_dep1)<5 & !missing(gSmallFirm1) & !missing(bartikGS1) & !missing(Lcl_dep) & !missing(LmHQ) & !missing(Lacq_dep)

*local controls ln_pop_2000 unem_2000 urban_2000 lnincome2000 lnbr2000 small_dep_share2000
local controls 	dlngdp unem_change pop_growth annualchange 


clear matrix
capture file close fh
file open fh  using "$LocalData\Tables\Adao_Standard_Errors_MergersRFS.tex", write replace
file write fh "\toprule" _n
file write fh  " (1) & (2)  & (3)  & (4)  & (5) & (6) & (7) & (8) & (9) " _n

eststo: reg L.cl_dep  gSmallFirm1 `controls' i.year if `years', vce(cluster cluster3)
outreg2 using "$LocalData\Tables\BartikRegTable_10RFS", tex replace ///
	ctitle(AcqDep) keep(gSmallFirm1  `controls' ) ///
	addtext(REG, OLS, YEAR FE, YES, YRS, 2003-2017)
	
eststo: areg L.cl_dep  gSmallFirm1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_10RFS", tex append ///
	ctitle(AcqDep) keep(gSmallFirm1  `controls' ) ///
	addtext(REG, OLS, YEAR FE, YES, YRS, 2003-2017)
	
eststo: xtivreg2 L.cl_dep  year_* (gSmallFirm1  = bartikGS1  ) if `years', fe cluster(cluster3)
	local widstat = round(`e(widstat)',0.1)	
outreg2 using "$LocalData\Tables\BartikRegTable_10RFS", tex append ///
	ctitle(AcqDep) keep(gSmallFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, YRS, 2003-2017)

 	preserve

		keep if `years' & Lcl_dep!=.
		
		drop year_*
		drop geography_* 
		
		levelsof year, local(y) //Creates a macro list of all years
		qui tab geography, gen(geography_) //Creates a macro list of all years
		qui tab year, gen(year_) //Creates a macro list of all years
		drop year_1 geography_1

		drop t*_c_i_share_2000*
				foreach year in `y' {
			foreach ind_var of varlist c_i_share_2000* {
				qui gen t`year'_`ind_var' = `ind_var' * (year == `year')
				}
			}

		local controls2 year_* geography_* 
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		
		noi Adao Lcl_dep bartikGS1 `controls2' if `filter', wmatrix(`weightmatrix') tsls(gSmallFirm1)
	
		local se3=string(se_alpha[1,1], "%9.2f")
		local ciL3 =string(ciL2SLS[1,1], "%9.2f")
		local ciH3 =string(ciH2SLS[1,1], "%9.2f")

	restore	
	
eststo: reg L.mHQ  gSmallFirm1 `controls' i.year if `years', vce(cluster cluster3)
outreg2 using "$LocalData\Tables\BartikRegTable_10RFS", tex append ///
	ctitle(AcqdHQ) keep(gSmallFirm1  `controls' ) ///
	addtext(REG, OLS, YEAR FE, YES, YRS, 2003-2017)
	
	
eststo: areg L.mHQ  gSmallFirm1  i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_10RFS", tex append ///
	ctitle(AcqdHQ) keep(gSmallFirm1  `controls' ) ///
	addtext(REG, OLS, YEAR FE, YES, YRS, 2003-2017)

eststo: xtivreg2 L.mHQ    year_* (gSmallFirm1  = bartikGS1  ) if `years', fe cluster(cluster3)
	local widstat = round(`e(widstat)',0.1)	
outreg2 using "$LocalData\Tables\BartikRegTable_10RFS", tex append ///
	ctitle(AcqdHQ) keep(gSmallFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, YRS, 2003-2017)


 	preserve

		
			keep if `years' & LmHQ!=.

			drop year_*
		drop geography_* 
		
		levelsof year, local(y) //Creates a macro list of all years
		qui tab geography, gen(geography_) //Creates a macro list of all years
		qui tab year, gen(year_) //Creates a macro list of all years
		drop year_1 geography_1

		drop t*_c_i_share_2000*
				foreach year in `y' {
			foreach ind_var of varlist c_i_share_2000* {
				qui gen t`year'_`ind_var' = `ind_var' * (year == `year')
				}
			}

		local controls2 year_* geography_* 
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		
		noi Adao LmHQ bartikGS1 `controls2' if `filter', wmatrix(`weightmatrix') tsls(gSmallFirm1)
	
		local se6=string(se_alpha[1,1], "%9.2f")
		local ciL6 =string(ciL2SLS[1,1], "%9.2f")
		local ciH6 =string(ciH2SLS[1,1], "%9.2f")

	restore	

file write fh  " & `se1' & `se2' & `se3' & `se4' & `se5' & `se6' & `se7' & `se8' & `se9' & `se10'  \\" _n
file write fh  " & [`ciL1', `ciH1'] & [`ciL2', `ciH2'] & [`ciL3', `ciH3'] & [`ciL4', `ciH4'] & [`ciL5', `ciH5'] & [`ciL6', `ciH6'] & [`ciL7', `ciH7'] & [`ciL8', `ciH8'] & [`ciL9', `ciH9'] & [`ciL10', `ciH10']    \\" _n
	file close fh
	
	
/*
ivreg2 L.cl_dep  `controls' i.year (gSmallFirm1  = bartikGS1  ) if `years', cluster(state)
outreg2 using "$LocalData\Tables\BartikRegTable_5", append ctitle(Acquired) keep(gSmallFirm1  `controls' ) ///
	addtext(REG, Tobit, YEAR FE, YES, YRS, 2003-2017)
*/

eststo: reg L.acq_dep  gSmallFirm1 `controls' i.year if `years', vce(cluster cluster3)
outreg2 using "$LocalData\Tables\BartikRegTable_10RFS", tex append ///
	ctitle(BuyerDep) keep(gSmallFirm1  `controls' ) ///
	addtext(REG, OLS, YEAR FE, YES, YRS, 2003-2017)

eststo: areg L.acq_dep  gSmallFirm1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_10RFS", tex append ///
	ctitle(BuyerDep) keep(gSmallFirm1  `controls' ) ///
	addtext(REG, OLS, YEAR FE, YES, YRS, 2003-2017)
	
eststo: reg L.fail_dep  gSmallFirm1 `controls' i.year if `years', vce(cluster cluster3)
outreg2 using "$LocalData\Tables\BartikRegTable_10RFS", tex append ///
	ctitle(FailDep) keep(gSmallFirm1  `controls' ) ///
	addtext(REG, OLS, YEAR FE, YES, YRS, 2003-2017)

eststo: areg L.fail_dep  gSmallFirm1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_10RFS", tex append ///
	ctitle(FailDep) keep(gSmallFirm1  `controls' ) ///
	addtext(REG, OLS, YEAR FE, YES, YRS, 2003-2017)
	
 
noi display "Finished running bank balance sheets tests."
noi etime
noi display " "

noi display "Finished running merger regressions."
noi etime
noi display " "
